package com.example.game.module.service;

import com.example.game.module.entity.Category;
import com.example.game.module.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigInteger;
import java.util.Collections;
import java.util.List;

@Service
public class CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    public List<BigInteger> getCategoryIdByKeyword(String keyword) {

        if (keyword == null || keyword.trim().isEmpty()) {
            return Collections.emptyList();
        }
        return categoryMapper.findCategoryIdByKeyword(keyword);
    }

    public List<Category> listByIds(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }
        return categoryMapper.selectByIds(ids);
    }
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    public Category getById(Long categoryId) {
        return categoryMapper.getById(categoryId);
    }

    public List<Category> extractById(Long categoryId) {
        return categoryMapper.extractById(categoryId);
    }

    public Integer insert(Category entity) {
        return categoryMapper.insert(entity);
    }

    public Integer update(Category entity) {
    return categoryMapper.update(entity);
    }

    public Integer countChildren(Long categoryId) {
        return categoryMapper.countChildren(categoryId);
    }
    public Integer delete(Long categoryId) {

        if (categoryMapper.countChildren(categoryId)>0){
            throw new RuntimeException("请先删除子分类");
        }
        return categoryMapper.delete((int)(System.currentTimeMillis() / 1000),categoryId);
    }
}

